package com.wuliu.dao.impl;

import com.wuliu.dao.BusinessClassDAO;
import com.wuliu.model.BusinessClass;
import com.wuliu.util.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class BusinessClassDAOImpl implements BusinessClassDAO {
    @Override
    public void add(BusinessClass businessClass) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="insert into business_class values(null,?,?,?)";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,businessClass.getName());
            statement.setString(2,businessClass.getDesc());
            statement.setString(3,businessClass.getImg());
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public void deleteById(int id) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="delete from business_class where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(BusinessClass businessClass) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="update business_class set name=? ,img=?,`desc`=?where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,businessClass.getName());
            statement.setString(2,businessClass.getImg());
            statement.setString(3,businessClass.getDesc());
            statement.setInt(4,businessClass.getId());


            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public BusinessClass findById(int id) {
        BusinessClass businessClass=null;
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from business_class where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                String name=set.getString("name");
                String desc =set.getString("desc");
                String img =set.getString("img");
                businessClass=new BusinessClass(id,name,desc,img);

            }
            set.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return businessClass;

    }

    @Override
    public List<BusinessClass> findAll() {
        List<BusinessClass> businessClasses=new ArrayList<>();
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection =jdbc.connection();
            String sql="select * from business_class";
            PreparedStatement statement=connection.prepareStatement(sql);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                int id =set.getInt("id");
                String name=set.getString("name");
                String img=set.getString("img");
                String desc=set.getString("desc");
                BusinessClass businessClass=new BusinessClass(id,name,img,desc);
                businessClasses.add(businessClass);
            }
            set.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return businessClasses;
    }



}
